home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr27 / ct.zip / CT.CPP < prev    next >
C/C++ Source or Header  |  1995-05-29  |  8KB  |  310 lines

  1. /****************************** COUNT-UP TIMER******************************
  2. Count-up timer                                      Digital displays of time
  3.  
  4.    Custom version created for Jim Magin, Ellicott City, MD per his specs.
  5.  
  6.                                    M\Cooper
  7.                            3425 Chestnut Ridge Rd.
  8.                          Grantsville, MD  21536-9801
  9.                          ---------------------------
  10.                          Email:   thegrendel@aol.com
  11. ****************************************************************************/
  12.  
  13. /* #define DEBUG */
  14.  
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include <conio.h>
  18. #include <stdlib.h>
  19. #include <graphics.h>
  20. #include <dos.h>
  21. #include "oscr.hpp"
  22. #include "bt.hpp"
  23.  
  24.  
  25. char R[] =  "Press a key to reset timer";
  26. char S[] =  "Press a key to restart timer";
  27. char ST[] = "RETURN stop | SPACE restart";
  28. void main()
  29. {
  30.    randomize();
  31.    opening_screen();
  32.    play();
  33. }
  34.  
  35. void CountdownTimer::clock_on()
  36. {
  37.    time_t prev_sec;
  38.    int ch;
  39.  
  40.       text_color = LIGHTRED;
  41.  
  42.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  43.       settextjustify( LEFT_TEXT, TOP_TEXT );
  44.  
  45.       startn_t = time ( NULL );  //Click on stopwatch.
  46.       setcolor( BKGRND_COLOR );
  47.       outtextxy( NAME_POS, 100, R );
  48.       outtextxy( NAME_POS, 100, S );
  49.       outtextxy( NAME_POS, 100, ST );
  50.       setcolor( LIGHTBLUE );
  51.       outtextxy( NAME_POS, 100, ST );
  52.  
  53.  
  54. /***************************AAAAA*************************************/
  55.  
  56.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, BIGNUMSIZE );
  57.       display_time();  //Otherwise initial time not displayed...
  58.  
  59.       while( !( ch = kbhit() ) )
  60.      {
  61.      prev_sec = seconds;
  62.      interval_t = time( NULL ) - start_t;
  63.      running_t = total_seconds + interval_t;
  64.      convert( running_t );
  65.  
  66.      if( seconds - prev_sec )
  67.         {
  68.         display_time();
  69.  
  70.         if( !seconds )
  71.            if( minutes == warning )
  72.           if( !hours )
  73.              if( time_warning_flag )
  74.             blatt();
  75.  
  76.         if( visual_ticking_flag ) // Show blinking box ticks?
  77.            {
  78.            setfillstyle( random ( PATTERNS ), random ( COLORS ) );
  79.            setcolor ( random ( COLORS ) );
  80.            setlinestyle( SOLID_LINE, 0xFFF, NORM_WIDTH );
  81.            bar( X_C - RADIUS, Y_C - RADIUS,
  82.             X_C + RADIUS, Y_C + RADIUS );
  83.            }
  84.         }
  85.  
  86.  
  87.      }
  88.  
  89.       ch = getch();
  90.       if( ch == ESC )
  91.      exit__();  // Quit.
  92.       
  93.       if( ch == CR )
  94.          {
  95.       running_flag = OFF; // Reset each time.
  96.       setcolor( BKGRND_COLOR );
  97.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  98.       outtextxy( NAME_POS, 100, S );
  99.       outtextxy( NAME_POS, 100, ST );
  100.       outtextxy( NAME_POS, 100, R );
  101.       setcolor( CYAN );
  102.       outtextxy( NAME_POS, 100, S );
  103.       ch = getch();
  104.       if( ch == ESC )
  105.      exit__(); // Quit.
  106.       }
  107.       reset_timer(); 
  108.  
  109.       return;
  110.  
  111. }
  112.  
  113. void CountdownTimer::display_moves()
  114. {
  115.    char buf[ 5 ];
  116.    static char ebuf[ 5 ];
  117.  
  118.       if( moves > 1 )
  119.      {
  120.      setcolor( WHITE );
  121.      settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  122.      settextjustify( CENTER_TEXT, TOP_TEXT );
  123.      outtextxy( MOVES_X, MOVES_Y, ebuf );
  124.      }
  125.  
  126.       sprintf( buf, "%003d", moves );
  127.       sprintf( ebuf, buf );
  128.       setcolor( GREEN );
  129.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  130.       settextjustify( CENTER_TEXT, TOP_TEXT );
  131.       outtextxy( MOVES_X, MOVES_Y, buf );
  132.       
  133.       return;
  134. }
  135.  
  136. void graphics_setup( int background_color )
  137. {
  138.    int grdriver = VGA,
  139.        grmode = VGAHI;
  140.  
  141.        registerfarbgidriver( EGAVGA_driver_far );
  142.        registerfarbgifont( gothic_font_far );
  143.        registerfarbgifont( triplex_font_far );
  144.        initgraph( &grdriver, &grmode, "" );
  145.        setbkcolor( background_color );
  146.  
  147. }
  148.  
  149. void exit__()
  150. {
  151.       closegraph();
  152.       exit( QUIT );
  153. }
  154.  
  155.     /***************Routine to erase old numbers*************/
  156. void CountdownTimer::erase_numbers()
  157. {
  158.       #ifdef DEBUG
  159.          setcolor( CYAN );
  160.       #else
  161.          setcolor ( WHITE ); 
  162.       #endif
  163.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, BIGNUMSIZE );
  164.  
  165.       if( seconds == 0 )        //At minute intervals
  166.      outtextxy( BLK_TIME, Y_TIMEPOS, line_clear );
  167.       else
  168.       if( !( seconds % 10 ) )   //At 10 second intervals
  169.         outtextxy( BLK_TIME + POS1_OFFSET, Y_TIMEPOS,
  170.                line_clear + 6 );
  171.       else                         //At second intervals
  172.          outtextxy( BLK_TIME + POS_OFFSET, Y_TIMEPOS,
  173.             line_clear + 7 ); 
  174.  
  175.      return;
  176.  
  177. }
  178.  
  179.  
  180. void play()
  181. {
  182.    int hrs,
  183.        min;
  184.    char inputstr[ MAXLEN ],
  185.     inp;
  186.  
  187.       clrscr();
  188.  
  189.       textcolor ( RED );
  190.       CountdownTimer t1( 0 );
  191.  
  192.       textcolor( CYAN );
  193.       cprintf( "\n\n                         Enable flashing clock ticks? " );
  194.       inp = getche();
  195.       if( inp == 'y' || inp == 'Y' )
  196.      t1.visual_ticking_flag = ON;
  197.       else
  198.      t1.visual_ticking_flag = OFF;
  199.  
  200.       cprintf( "\n                                                      Enable time warning? " );
  201.       inp = getche();
  202.       if( inp == 'y' || inp == 'Y' )
  203.      {
  204.      t1.time_warning_flag =  ON;
  205.      cprintf( "                                                          At how many minutes? " );
  206.      gets( inputstr );
  207.      t1.warning = atoi( inputstr );
  208.      }
  209.       else
  210.      t1.time_warning_flag = OFF;
  211.  
  212.       textcolor( YELLOW | BLINK );
  213.       _setcursortype( _NOCURSOR );
  214.       printf( "\n\n\n\n\n\n\n\n\n\n\n\n" );
  215.       cprintf( "                             PRESS A KEY TO BEGIN" );
  216.       while ( !getch() );
  217.  
  218.       graphics_setup( WHITE );
  219.  
  220.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  221.       settextstyle( GOTHIC_FONT, HORIZ_DIR, 4 );
  222.       setcolor( LIGHTMAGENTA );
  223.       outtextxy( TOPX, TOPY - 30, title_msg );  /*******************/
  224.  
  225.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  226.       setcolor( LIGHTGREEN );
  227.       outtextxy( TOPX, TOPY + 20, esc_msg );
  228.  
  229.       t1.initialize_clock();
  230.       t1.moves++;
  231.       t1.display_moves();
  232.       t1.clock_on();
  233.  
  234.       while ( PLAY )  //Forever, until keypress
  235.      {
  236.      t1.moves++;  
  237.      t1.display_moves();
  238.      t1.clock_on();
  239.      }
  240.  
  241.  
  242. } // End play()
  243.  
  244.  
  245. void opening_screen()
  246. {
  247.    char topline[] = "Count Up Timer",
  248.     by_line[] = "by",
  249.     name_line[] = "M\\Cooper",
  250.     endline[] = "PRESS A KEY TO BEGIN";
  251.  
  252.       graphics_setup( LIGHTCYAN );
  253.       settextstyle( GOTHIC_FONT, HORIZ_DIR, HEADLINE_SIZE );
  254.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  255.       setcolor( LIGHTRED );
  256.       outtextxy( TOPX, TOPY, topline );
  257.  
  258.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, BY_LINE_SIZE );
  259.       setcolor( BLUE );
  260.       outtextxy( BY_LINE_X, BY_LINE_Y, by_line );
  261.  
  262.       setfillstyle( BAR_PATTERN, BAR_COLOR );
  263.       bar3d( BAR_LEFT, BAR_TOP, BAR_RIGHT, BAR_BOTTOM, BAR_DEPTH, BAR_TOPFLAG );
  264.  
  265.       setfillstyle( PIE_PATTERN, PIE_COLOR );
  266.       pieslice( PIE1_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  267.       circle( PIE1_X, PIE_Y, CIRC_RAD );
  268.       setlinestyle( SOLID_LINE, 0xFFFF, THICK_WIDTH );
  269.       setcolor( LIGHTRED );
  270.       line( LINE1_X, LINE_Y1, LINE1_X, LINE_Y2 );
  271.       setfillstyle( PIE_PATTERN, WHITE );
  272.       bar( B1_LEFT, B1_TOP, B1_RIGHT, B_BOTTOM );
  273.  
  274.  
  275.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, NAME_LINE_SIZE );
  276.       setcolor( BLUE );
  277.       outtextxy( NAME_LINE_X, NAME_LINE_Y, name_line );
  278.  
  279.       sleep( DELAY / 2 );
  280.  
  281.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, ENDLINE_SIZE );
  282.       setcolor( RED );
  283.       outtextxy( ENDLINE_X, ENDLINE_Y, endline );
  284.  
  285.       getch();
  286.       closegraph();
  287.  
  288.       return;
  289. }
  290.  
  291.  
  292. void CountdownTimer::erase()
  293. {
  294.       display_time();
  295.       setcolor( WHITE );
  296.       outtextxy( BLK_TIME, Y_TIMEPOS, line_clear ); /* Erase time display */
  297.  
  298. }
  299.  
  300. void CountdownTimer::reset_timer()
  301. {
  302.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  303.       erase();
  304.       convert ( total_seconds_mem ); /***RESET TIME*****/
  305.       initialize_clock();
  306.  
  307.       return;
  308.  
  309. }
  310.